import { ProTable } from '@ant-design/pro-components'
import { Drawer, Button } from 'antd'
import { useState } from 'react'
import { useRequest } from 'ahooks'
import { Http } from '../../../Api'
import { API_LIST } from '../../../Api/Api'
import request from 'umi-request'
import { commonTableList, BuiltInData } from '../Common/common'
import { useEffect } from 'react'
import { InfoTypeManageTree } from '../Common/Conversion'

const ClientData = JSON.parse(localStorage.getItem('ClientData'))

const synced = {
    dataIndex: 'synced',
    title: '状态',
    valueType: 'select',
    valueEnum: { 0: '未同步', 1: '已同步' },
    width: 100,
}

const urlData = {
    posBCCenterPushPushCenterCamera: {
        title: '上传摄像头表数据，及摄像头分组关系数据(VD_Camera)',
        url: API_LIST()['getCamera'],
        params: { pageIndex: 1 },
        columns: [
            commonTableList.uuid,
            { ...commonTableList.tableName, title: '摄像头名称' },
            { ...commonTableList.organizationUuid, valueEnum: ClientData.organization },
            {
                dataIndex: 'deviceSerial',
                title: '设备序列号',
                valueType: 'input',
                rules: true,
                width: 150,
            },
            {
                dataIndex: 'channelNo',
                title: '通道号',
                valueType: 'input',
                hideInSearch: true,
                rules: true,
                width: 100,
            },
            synced,
        ],
    },
    posBCCenterPushPushCenterVideo: {
        title: '上传视频表数据(VD_Video)',
        url: API_LIST()['getVideo'],
        params: { pageIndex: 1 },
        columns: [
            commonTableList.uuid,
            {
                dataIndex: 'tableName',
                title: '视频名称',
                valueType: 'input',
                rules: true,
                width: 200,
            },
            { ...commonTableList.organizationUuid, valueEnum: ClientData.organization },
            {
                dataIndex: 'videoDate',
                title: '拍摄日期',
                valueType: 'Date',
                showTime: true,
                rules: true,
                hideInSearch: true,
                width: 200,
            },
            synced,
        ],
    },
    posBCCenterPushPushCenterSecurity: {
        title: '上传安全检查表数据，及附件数据，整改表，及整改表附件数据(SC_Security)',
        url: API_LIST()['getSecurity'],
        params: { pageIndex: 1 },
        columns: [
            commonTableList.uuid,
            { ...commonTableList.tableName, title: '巡检主题' },
            { ...commonTableList.organizationUuid, valueEnum: ClientData.organization },
            {
                dataIndex: 'problem',
                title: '问题描述',
                valueType: 'input',
                rules: true,
                width: 250,
                hideInSearch: true,
            },
            {
                dataIndex: 'securityDate',
                title: '巡检日期',
                valueType: 'Date',
                showTime: true,
                rules: true,
                width: 150,
                hideInSearch: true,
            },
            synced,
        ],
    },
    posBCCenterPushPushCenterAttachment: {
        title: '上传附件表数据(Sys_Attachment)',
        url: API_LIST()['getAttachment'],
        params: { pageIndex: 1 },
        columns: [
            commonTableList.uuid,
            {
                dataIndex: 'fileType',
                title: '文件类型',
                rules: true,
                width: 100,
                valueType: 'select',
                valueEnum: BuiltInData.FileType,
            },
            commonTableList.tableName,
            synced,
        ],
    },
    posBCCenterPushPushLabStressMachine: {
        title: '上传压力机抗折抗压一体机数据(LabStressMachine)',
        url: API_LIST()['getLabStressMachineOC'],
        params: { pageIndex: 1, OTestDate: 'desc@0' },
        columns: [
            commonTableList.uuid,
            { ...commonTableList.organizationUuid, valueEnum: ClientData.organization },
            {
                dataIndex: 'testType',
                title: '试验类别',
                valueType: 'input',
                width: 250,
            },
            {
                dataIndex: 'testDate',
                title: '试验日期',
                hideInSearch: true,
                valueType: 'input',
                width: 150,
            },
            {
                dataIndex: 'testEvaluation',
                title: '是否合格',
                valueType: 'input',
                render: (_, e) => <div>{e.testEvaluation === 0 ? '合格' : '不合格'}</div>,
                width: 150,
            },
            synced,
        ],
    },
    posBCCenterPushPushLabUniversalRebars: {
        title: '上传万能机数据(LabUniversalRebar)',
        url: API_LIST()['getLabUniversalRebarOC'],
        params: { pageIndex: 1, OTestDate: 'desc@0' },
        columns: [
            commonTableList.uuid,
            { ...commonTableList.organizationUuid, valueEnum: ClientData.organization },
            {
                dataIndex: 'testType',
                title: '试验类别',
                hideInSearch: true,
                valueType: 'input',
                width: 200,
            },
            {
                dataIndex: 'testDate',
                title: '试验时间',
                hideInSearch: true,
                valueType: 'input',
                width: 200,
            },
            {
                dataIndex: 'result',
                title: '是否合格',
                valueType: 'input',
                width: 150,
            },
            synced,
        ],
    },
    posBCCenterPushPushLabHumiture: {
        title: '上传标养室数据(LabHumiture)',
        url: API_LIST()['getLabHumitureOC'],
        params: { pageIndex: 1 },
        columns: [
            commonTableList.uuid,
            { ...commonTableList.organizationUuid, valueEnum: ClientData.organization },
            {
                dataIndex: 'sensorName',
                title: '传感器名称',
                hideInSearch: true,
                valueType: 'input',
                width: 150,
            },
            {
                dataIndex: 'dataCreationTime',
                title: '创建时间',
                valueType: 'input',
                hideInSearch: true,
                width: 150,
            },
            synced,
        ],
    },
    posBCCenterPushPushLabReboundHammer: {
        title: '上传回弹仪数据(LabReboundHammer)',
        url: API_LIST()['getLabReboundHammerOC'],
        params: { pageIndex: 1 },
        columns: [
            commonTableList.uuid,
            { ...commonTableList.organizationUuid, valueEnum: ClientData.organization },
            {
                dataIndex: 'componentName',
                title: '构件名称',
                valueType: 'input',
                width: 200,
            },
            {
                dataIndex: 'detectionTime',
                title: '检测时间',
                valueType: 'input',
                hideInSearch: true,
                width: 180,
            },
            synced,
        ],
    },
    posBCCenterPushPushLabCoverProtector: {
        title: '上传钢筋保护层数据(LabCoverProtector)',
        url: API_LIST()['getLabCoverProtectorOC'],
        params: { pageIndex: 1 },
        columns: [
            commonTableList.uuid,
            { ...commonTableList.organizationUuid, valueEnum: ClientData.organization },
            {
                dataIndex: 'structuralPosition',
                title: '结构位置',
                valueType: 'input',
                width: 150,
            },

            {
                dataIndex: 'detectionTime',
                title: '检测时间',
                valueType: 'input',
                width: 180,
                hideInSearch: true,
            },
            synced,
        ],
    },
    posBCCenterPushPushLabMarshallStability: {
        title: '上传马歇尔数据(LabMarshallStability)',
        url: API_LIST()['getLabMarshallStabilityOC'],
        params: { pageIndex: 1, OTestDate: 'desc@0' },
        columns: [
            commonTableList.uuid,
            { ...commonTableList.organizationUuid, valueEnum: ClientData.organization },
            {
                dataIndex: 'sampleName',
                title: '样品名称',
                valueType: 'input',
                width: 150,
            },
            {
                dataIndex: 'startTime',
                title: '开始时间',
                valueType: 'input',
                hideInSearch: true,
                width: 150,
            },
            {
                dataIndex: 'endTime',
                title: '结束时间',
                valueType: 'input',
                hideInSearch: true,
                width: 150,
            },
            {
                dataIndex: 'result',
                title: '是否合格',
                valueType: 'input',
                render: (_, e) => <div>{e.result === 0 ? '合格' : '不合格'}</div>,
                width: 150,
            },
            synced,
        ],
    },
    posBCCenterPushPushLabDuctility: {
        title: '上传延度仪数据(LabDuctility)',
        url: API_LIST()['getLabDuctilityOC'],
        params: { pageIndex: 1 },
        columns: [
            commonTableList.uuid,
            { ...commonTableList.organizationUuid, valueEnum: ClientData.organization },
            {
                dataIndex: 'sampleName',
                title: '样品名称',
                valueType: 'input',
                width: 250,
            },
            {
                dataIndex: 'startTime',
                title: '开始时间',
                valueType: 'input',
                width: 250,
                hideInSearch: true,
            },
            {
                dataIndex: 'endTime',
                title: '结束时间',
                valueType: 'input',
                width: 250,
                hideInSearch: true,
            },
            synced,
        ],
    },
    posBCCenterPushPushLabSofteningPoint: {
        title: '上传软化点数据(LabSofteningPoint)',
        url: API_LIST()['getLabSofteningPointOC'],
        params: { pageIndex: 1, OTestDate: 'desc@0' },
        columns: [
            commonTableList.uuid,
            { ...commonTableList.organizationUuid, valueEnum: ClientData.organization },
            {
                dataIndex: 'sampleName',
                title: '样品名称',
                valueType: 'input',
                width: 150,
            },
            {
                dataIndex: 'startTime',
                title: '开始时间',
                valueType: 'input',
                hideInSearch: true,
                width: 150,
            },
            {
                dataIndex: 'endTime',
                title: '结束时间',
                valueType: 'input',
                hideInSearch: true,
                width: 150,
            },
            {
                dataIndex: 'result',
                title: '是否合格',
                valueType: 'input',
                render: (_, e) => <div>{e.result === 0 ? '合格' : '不合格'}</div>,
                width: 150,
            },
            synced,
        ],
    },
    posBCCenterPushPushLabPenetration: {
        title: '上传针入度数据(Lab_LabPenetration)',
        url: API_LIST()['getLabPenetrationOC'],
        params: { pageIndex: 1, OTestDate: 'desc@0' },
        columns: [
            commonTableList.uuid,
            { ...commonTableList.organizationUuid, valueEnum: ClientData.organization },
            {
                dataIndex: 'sampleName',
                title: '样品名称',
                valueType: 'input',
                width: 150,
            },
            {
                dataIndex: 'startTime',
                title: '开始时间',
                valueType: 'input',
                hideInSearch: true,
                width: 150,
            },
            {
                dataIndex: 'endTime',
                title: '结束时间',
                valueType: 'input',
                hideInSearch: true,
                width: 150,
            },
            {
                dataIndex: 'result',
                title: '是否合格',
                valueType: 'input',
                render: (_, e) => <div>{e.result === 0 ? '合格' : '不合格'}</div>,
                width: 150,
            },
            synced,
        ],
    },
    posBCCenterPushPushLabPmsTesterCbr: {
        title: '上传CBR数据(LabPmsTesterCbr)',
        url: API_LIST()['getLabPmsTesterCbrOC'],
        params: { pageIndex: 1 },
        columns: [
            commonTableList.uuid,
            { ...commonTableList.organizationUuid, valueEnum: ClientData.organization },
            {
                dataIndex: 'sampleName',
                title: '样品名称',
                valueType: 'input',
                width: 150,
            },
            {
                dataIndex: 'startTime',
                title: '开始时间',
                valueType: 'input',
                width: 150,
                hideInSearch: true,
            },
            synced,
        ],
    },
    posBCCenterPushPushInfoManage: {
        title: '上传文件柜数据(Info_InfoManage)',
        url: API_LIST()['getInfoManage'],
        params: { pageIndex: 1 },
        columns: [
            commonTableList.uuid,
            {
                dataIndex: 'infoTypeManageUuid',
                title: '文件类型',
                valueType: 'treeSelect',
                fieldProps: {
                    options: [],
                    fieldNames: { label: 'tableName', value: 'uuid' },
                },
                width: 150,
            },
            { ...commonTableList.organizationUuid, valueEnum: ClientData.organization },
            {
                dataIndex: 'tableName',
                title: '主题',
                valueType: 'input',
                width: 150,
            },
            synced,
        ],
    },
}

export const BCCDrawerBox = ({ Off, url, BCCenterPush, UpdateSelect }) => {
    const [headers] = useState({ Authorization: sessionStorage.getItem('TOKEN') })

    const [columns, setColumns] = useState()

    const [paramsData, setParamsData] = useState()
    const Submit = e => {
        const data = e.map(({ uuid }) => uuid)
        BCCenterPush({ url, path: [UpdateSelect, data.join(','), 1] })
    }

    useEffect(() => {
        const data = async () => {
            const { columns } = urlData[url]
            columns.find(v => v.dataIndex === 'infoTypeManageUuid').fieldProps.options = await InfoTypeManageTree()
            setColumns(columns)
        }

        if (url === 'posBCCenterPushPushInfoManage') {
            data()
        } else setColumns(urlData[url].columns)
    }, [url])

    return (
        <Drawer title={`${urlData[url].title}`} placement="right" width={1000} onClose={Off} open={true}>
            {columns && (
                <ProTable
                    columns={columns}
                    rowSelection={{ type: 'checkbox' }}
                    params={urlData[url].params}
                    tableAlertOptionRender={({ selectedRows }) => (
                        <Button type="link" onClick={() => Submit(selectedRows)}>
                            保存
                        </Button>
                    )}
                    request={async (params = urlData[url].params) => {
                        delete params.current
                        params.pageIndex = paramsData || 1
                        return request(urlData[url].url, { params, headers }).then(res => {
                            return { data: res.list, total: res.total, success: true }
                        })
                    }}
                    editable={{ type: 'multiple' }}
                    pagination={{
                        pageSize: urlData[url].pageSize,
                        showSizeChanger: true,
                        onChange: e => setParamsData(e),
                    }}
                    columnsState={{ persistenceKey: 'pro-table-singe-demos', persistenceType: 'localStorage' }}
                    rowKey="uuid"
                    search={true}
                    headerTitle="筛选结果"
                    scroll={{ y: 550 }}
                />
            )}
        </Drawer>
    )
}
